<?php
	include("header.php");
	
	require_once("lib/statutory.php");
	require_once("lib/sql.php");
	require_once("lib/util.php");
?>
<?php

	$pageTitle ="View Statutory List";
			
	$util=new DBUtil();
	$conn=$util->getConnection();
	$sql = SQLQuery::STATUTORY_ALL_SQL;
	$stmt = $conn->prepare( $sql);
	$stmt->execute();
			
	$result = $stmt->get_result();
	$statutories = array();
			
	if ($result->num_rows > 0) {
		while($row = $result->fetch_array()) {
		$statutory = new Statutory();
		$statutory->setId($row["ID"]);
		$statutory->setName($row["NAME"]);
		$statutory->setDescription($row["DESCRIPTION"]);
		array_push($statutories,$statutory);
			}
			}
	$conn->close();
			

?>


<script type="text/javascript">
function updateDetails(){

	//Delete all rows except the first row(index 0)
	$("#statutorydetails > div > div> table > tbody > tr").each(function(index){
		if(index > 0){
			$(this).remove();
		}
	});
	//Get the data from backend
	$.getJSON("lib/managestatutory.php?fulldetails=yes",function(data,status){
	
		$.each(data, function(counter, statutory) {
			var statutoryId=statutory.id;
			var statutoryName=statutory.name;
			var statutoryDescription=statutory.description;
 
			var bodyelement=$("#statutorydetails > div > div> table > tbody");
			var updateStr= '<a id="statutoryId'+statutoryId+ '" href="#" onclick="javascript:updateDialog(\'Update Statutory\',\'lib/managestatutory.php\',\'lib/managestatutory.php\',\''+statutoryId+'\',\'updateDetails()\')"><span>Edit Statutory</span><div class="modify"></div></a>';
			var deleteStr= '<a id="statutoryId'+statutoryId+ '" href="#" onclick="javascript:deleteDialog(\'Delete Statutory\',\'lib/managestatutory.php\',\'lib/managestatutory.php\',\''+statutoryId+'\',\'updateDetails()\')"><span>Remove Statutory</span><div class="delete"></div></a>';
			var rowString='<tr><td>'+(counter+1) +'</td><td>'+statutoryName+'</td><td>'+statutoryDescription+'</td><td>'+updateStr+'</td><td>'+deleteStr+'</td></tr>';
			$(rowString).appendTo(bodyelement);
		});
		
		var pane = $('#statutorydetails');
		var api = pane.data('jsp');
		api.reinitialise();
	});
}
</script>

<div class="maincontent">
<!------- Header ---------->
<div class="borderedDiv" >
	<table style="width:100%">
		<tr>
			<td class="titleText blueheadertext">
				View Statutory
			</td>
		</tr>
	</table>
</div>


<!------- Content ---------->

<div class="scroll-pane displaytable" id="statutorydetails">
	<table class="contenttable">
	<thead>
		<tr>
			<td style="width:8%">Sl. No.</td>
			<td>Name</td>
			<td style="width:50%">Description</td>
			<td style="width:20px">&nbsp;</td>
			<td style="width:20px">&nbsp;</td>
		</tr>
	</thead>
	
	<tbody>
	<tr>
		<td colspan="5" style="text-align: right;"><a href="#" onClick="javascript:createDialog('Create Statutory', 'lib/managestatutory.php','lib/managestatutory.php','updateDetails()')"><span>Add Statutory</span><div id="addStatutory" class="add" style="float:right;"></div> </a></td>
	</tr>
	
<?php
	for($i=0;$i < sizeof($statutories);$i++){
		$statutory=$statutories[$i];
		$statutoryId=$statutory->getId();
		$statutoryName=$statutory->getName();
		$statutoryDescription=$statutory->getDescription();
				
		echo ("<tr>");
		echo ("<td>" .($i+1) ."</td>");
		echo ("<td>" .$statutoryName ."</td>");
		echo ("<td>" .$statutoryDescription ."</td>");
		echo "<td><a id=\"statutoryid_" .$statutoryId  ."\" alt=\"Edit\" href=\"#\" onClick=\"javascript:updateDialog('Update Statutory', 'lib/managestatutory.php','lib/managestatutory.php'," .$statutoryId .",'updateDetails()')\"><span>Edit Statutory</span><div class=\"modify\"></div></a></td>";
		echo "<td><a id=\"statutoryid_" .$statutoryId  ."\" href=\"#\"  onClick=\"javascript:deleteDialog('Delete Statutory', 'lib/managestatutory.php','lib/managestatutory.php'," .$statutoryId .",'updateDetails()')\"><span>Remove Statutory</span><div class=\"delete\"></div></a></td>";
		echo ("</tr>");

	}
?>
	</tbody>
	</table>
</div>
</div>
<?php
	include("footer.php");
?>